#include "include/FSCP_RealTimeTaskHelper.hpp"
#include "include/kernel.hpp"
#include "include/logger.hpp"

using namespace qOS;

// 示例任务函数
int task1Function(uint64_t token, TlsfYoungAgeString& syncTaskData, void *userData) {
    // 模拟任务执行
    static int counter = 0;
    counter++;
    
    logger::out(logger::info) << "Task1 executed, counter: " << counter;
    
    // 模拟一些工作
    for (volatile int i = 0; i < 1000; i++);
    
    return 0; // 成功返回0
}

int task2Function(uint64_t token, TlsfYoungAgeString& syncTaskData, void *userData) {
    static int counter = 0;
    counter++;
    
    logger::out(logger::info) << "Task2 executed, counter: " << counter;
    
    // 模拟一些工作
    for (volatile int i = 0; i < 500; i++);
    
    return 0;
}

int task3Function(uint64_t token, TlsfYoungAgeString& syncTaskData, void *userData) {
    static int counter = 0;
    counter++;
    
    logger::out(logger::info) << "Task3 executed, counter: " << counter;
    
    // 模拟一些工作
    for (volatile int i = 0; i < 200; i++);
    
    return 0;
}

// 空闲任务回调
void idleTaskCallback(event_t e, uint64_t securityToken) {
    if (e.firstCall()) {
        logger::out(logger::info) << "Idle task started";
    }
    
    // 安全令牌验证
    if (securityToken != e.thisTask().GetSecurityToken()) {
        return;
    }
    
    // 空闲任务可以执行一些低优先级的工作
    static int idleCounter = 0;
    if (idleCounter++ % 1000 == 0) {
        logger::out(logger::debug) << "Idle task running...";
    }
}

int main() {
    // 创建操作系统实例
    core os;
    
    // 初始化操作系统
    if (!os.init()) {
        logger::out(logger::error) << "Failed to initialize OS";
        return -1;
    }
    
    // 定义实时任务配置数组
    RealTimeTask rtTasks[] = {
        {
            .name = (char*)"HighPriorityTask",
            .runtime = 100000,      // 100微秒运行时间
            .period = 1000000,       // 1毫秒周期
            .deadline = 2000000,     // 2毫秒截止时间
            .action = 1,
            .WaitForSyncSignal = 0,
            .WaitForSyncZMQ = nullptr,
            .WaitForSyncPaxos = nullptr,
            .token = 0x12345678,
            .task_func = task1Function
        },
        {
            .name = (char*)"MediumPriorityTask",
            .runtime = 200000,       // 200微秒运行时间
            .period = 2000000,       // 2毫秒周期
            .deadline = 4000000,     // 4毫秒截止时间
            .action = 2,
            .WaitForSyncSignal = 0,
            .WaitForSyncZMQ = nullptr,
            .WaitForSyncPaxos = nullptr,
            .token = 0x87654321,
            .task_func = task2Function
        },
        {
            .name = (char*)"LowPriorityTask",
            .runtime = 50000,        // 50微秒运行时间
            .period = 5000000,       // 5毫秒周期
            .deadline = 10000000,     // 10毫秒截止时间
            .action = 3,
            .WaitForSyncSignal = 0,
            .WaitForSyncZMQ = nullptr,
            .WaitForSyncPaxos = nullptr,
            .token = 0xABCDEF12,
            .task_func = task3Function
        }
    };
    
    // 创建实时任务辅助器
    RealTimeTaskHelper rtHelper;
    
    // 初始化实时任务数组
    if (!rtHelper.initialize(rtTasks, sizeof(rtTasks) / sizeof(RealTimeTask))) {
        logger::out(logger::error) << "Failed to initialize RealTimeTaskHelper";
        return -1;
    }
    
    // 将任务添加到调度器
    if (!rtHelper.addTasksToScheduler(os)) {
        logger::out(logger::error) << "Failed to add tasks to scheduler";
        return -1;
    }
    
    // 添加空闲任务
    task idleTask;
    os.add(idleTask, idleTaskCallback, core::LOWEST_PRIORITY,
           clock::IMMEDIATE, task::PERIODIC, taskState::ENABLED_STATE);
    idleTask.setName("IdleTask");
    
    logger::out(logger::info) << "Real-time task system initialized successfully";
    logger::out(logger::info) << "Starting scheduler...";
    
    // 启动调度器
    os.run();
    
    return 0;
}